<template>
  <div id="rule-time-window" class="card col-12 px-0 mt-5">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb pb-0">
        <li class="breadcrumb-item ms-1" aria-current="page">基础数据</li>
        <li class="breadcrumb-item" aria-current="page">
          <router-link to="/h5/data/subclasses">舱位信息</router-link>
        </li>
        <li class="breadcrumb-item active" aria-current="page">
          {{ carrier }}退改时间
        </li>

        <span class="ms-auto me-1" @click.stop="back()">返回</span>
      </ol>
    </nav>

    <table class="table table-striped table-sm">
      <caption>
        修改航司退改条件的适用时间点
      </caption>
      <thead>
        <tr>
          <th class="col-2">序号</th>
          <th>最大时间</th>
          <th>最小时间</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="info of timeWindows">
          <td>
            {{ info.windowOrder }}
          </td>
          <td>
            <input
              type="text"
              class="text-center form-control"
              v-model.number="info.maxHours"
              :disabled="info.windowOrder === 1"
            />
            <div class="form-check form-check-inline">
              <input
                class="form-check-input"
                type="radio"
                value="0"
                v-model.number="info.maxIncluded"
                :disabled="info.windowOrder === 1"
              />
              <label class="form-check-label">不包含</label>
            </div>
            <div class="form-check form-check-inline">
              <input
                class="form-check-input"
                type="radio"
                value="1"
                v-model.number="info.maxIncluded"
                :disabled="info.windowOrder === 1"
              />
              <label class="form-check-label">包含</label>
            </div>
          </td>
          <td>
            <input
              type="text"
              class="text-center form-control"
              v-model.number="info.minHours"
              :disabled="info.windowOrder === 4"
            />
            <div class="form-check form-check-inline">
              <input
                class="form-check-input"
                type="radio"
                value="0"
                v-model.number="info.minIncluded"
                :disabled="info.windowOrder === 4"
              />
              <label class="form-check-label">不包含</label>
            </div>
            <div class="form-check form-check-inline">
              <input
                class="form-check-input"
                type="radio"
                value="1"
                v-model.number="info.minIncluded"
                :disabled="info.windowOrder === 4"
              />
              <label class="form-check-label">包含</label>
            </div>
          </td>
        </tr>
        <tr>
          <td colspan="3" class="text-center">
            <button
              type="button"
              class="btn btn-primary"
              @click.stop="saveTimeFrame()"
            >
              保存
            </button>
          </td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td colspan="3">
            <span class="text-muted">说明：0表示不限</span>
          </td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
import $ from "jquery";
import {
  searchFlightConditionTimeWindow,
  saveFlightConditionTimeWindow,
} from "@/api/basic-data.js";

export default {
  data() {
    return {
      carrier: "",
      timeWindows: [],
    };
  },
  computed: {
    isDataAdmin() {
      return this.$store.getters.isDataAdmin;
    },
  },
  mounted: function () {
    this.carrier = this.$route.query.carrier;
    this.search();
  },
  methods: {
    back: function () {
      this.$router.go(-1);
    },
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      searchFlightConditionTimeWindow(
        {
          carrier: this.carrier,
        },
        (v) => {
          this.timeWindows = v;
        }
      );
    },
    saveTimeFrame: function () {
      const jsonStr = JSON.stringify(this.timeWindows);
      // console.log(JSON.stringify(this.timeWindows))
      saveFlightConditionTimeWindow(jsonStr, (v) => {
        if (v.status === "OK") {
          this.showErrMsg("保存成功");
          this.back();
        } else {
          this.showErrMsg(v.errmsg);
        }
      });
    },
  },
};
</script>
